import {defaultConfig} from './constant';
import  globalConfig from '../globalConfig' ;
const DEFAULT_CONFIG = {
  faceidVerifySdkPageNum:1,
  baseUrl: 'https://faceid.qq.com',
  navTitle: {
    // 流程标题
    smsTitle: '短信验证',
    ocrTitle: '上传身份证', // 当手动输入身份证时，可能需要改为输入身份证
    livingbodyTitle: '录制视频',
    resultTitle: '验证结果',
    asrTitle: '意愿确认'
  },
  faceidVerifySdkToken:'',
  isHideAsrPage: false,
  cmsConfig: defaultConfig,
  curPage: 'sms',
  bizData:{},
  faceidVerifySdkHasGetResult: false,
}

const setInit = () => {
  console.log('setInit')
  uni.faceidVerifySdkPageNum = 1 ;
  uni.faceidVerifySdkHasGetResult = false ;
}

// faceidVerifySdk
const setToken = (token) => {
  uni.faceidVerifySdkToken = token ;
}

const setCmsConfig = (config) => {
  uni.faceidVerifySdkCmsConfig = config ;
}

const setBizdata = (bizData) => {
  uni.faceidVerifySdkBizData = bizData ;
}

const setNavTitle = (navTitle) => {
  uni.faceidVerifySdkNavTitle = navTitle ;
}

const setCurPage = (payload) => {
  const { url , params } = payload
  uni.faceidVerifySdkCurPage = url;
  console.log('uni.faceidVerifySdkPageNum',uni.faceidVerifySdkPageNum)
  if(!uni.faceidVerifySdkPageNum) {
    uni.faceidVerifySdkPageNum = 1 ;
  }
  let hasGetResult = uni.faceidVerifySdkHasGetResult;
  if(url == 'result') {
    hasGetResult = true ;
  }

  const _params =  params ? `?params=${JSON.stringify(params)}` : ''
  const _url = globalConfig.normalPath ? `${globalConfig.normalPath}/verify_mpsdk/pages/${url}/${url}` + _params : `/verify_mpsdk/pages/${url}/${url}` + _params
  console.log('_url',_url)
  if(uni.faceidVerifySdkHasGetResult){
    console.log('uni.faceidVerifySdkHasGetResult')
    uni.redirectTo({
      url: _url,
      success(){
        uni.faceidVerifySdkHasGetResult = hasGetResult ;
      }
    })
  }else{
    uni.navigateTo({
      url: _url,
      success(){
        uni.faceidVerifySdkHasGetResult = hasGetResult ;
        uni.faceidVerifySdkPageNum += 1  ;
      }
    })
  }


}

const setBaseUrl = (baseUrl) => {
  if(!uni.faceidVerifySdkBaseUrl || !baseUrl){
    uni.faceidVerifySdkBaseUrl = DEFAULT_CONFIG.baseUrl
  }else {
    uni.faceidVerifySdkBaseUrl = baseUrl
  }
}

export const $common = {
  setInit,
  setToken,
  setCmsConfig,
  setBizdata,
  setNavTitle,
  setCurPage,
  setBaseUrl
}